package test_jdom_read_XML;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import test_getResource.Test;

public class XMLReadUtil {

	private static String XML_PATH;

	public static String getXML_PATH() throws IOException {
		if (XML_PATH == null) {
			XML_PATH = XMLReadUtil.class.getResource("SSCconfig.xml").getPath();
		}
		return XML_PATH;
	}

	private static Map<String, Map<String, String>> xml2map;

	private static Map<String, Map<String, String>> getXml2map() {
		if (xml2map == null) {
			xml2map = new HashMap<String, Map<String, String>>();
		}
		return xml2map;
	}

	public static Map<String, Map<String, String>> readXml() {
		SAXBuilder saxBuilder = new SAXBuilder(false);
		Document doc;
		try {
			doc = (Document) saxBuilder.build(getXML_PATH());

			Element root = doc.getRootElement();
			List<Element> factory = root.getChildren("factory");

			for (Element e : factory) {
				String name = e.getAttributeValue("name");
				Map<String, String> map = new HashMap<String, String>();
				List<Element> ls = e.getChildren();
				for (Element ee : ls) {
					String nam = ee.getName();
					String value = ee.getValue();
					map.put(nam, value);
				}
				getXml2map().put(name, map);
			}
		} catch (JDOMException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return xml2map;
	}

	public static void main(String[] args) {
		Map map = XMLReadUtil.readXml();
		System.out.println("map");
	}
}
